#!/usr/bin/env python
# encoding: utf-8

from source.QuotaCalc.Interface import Quota, MarketUpdateSignal, QuotaUpdateSignal
from source.Lib.Tools import StockInfo
import threading
import time


def main():
    stocks = StockInfo()

    quotas = dict()
    for item in stocks.all_quota().values():
        quotas[item['id']] = Quota(item['id'], item['name'], item['formula'], item['sources'], item['market'])

    market_thread = threading.Thread(target=MarketUpdateSignal.Start, name='MarketUpdateSignal')
    quota_thread = threading.Thread(target=QuotaUpdateSignal.Start, name='QuotaUpdateSignal')

    market_thread.setDaemon(True)
    quota_thread.setDaemon(True)

    market_thread.start()
    quota_thread.start()

    # while 1:
    #     for item in stocks.all_quota().values():
    #         if item['id'] not in set(quotas.keys()):
    #             quotas[item['id']] = Quota(item['id'], item['name'], item['formula'], item['sources'], item['market'])
    #     time.sleep(15)

    market_thread.join()
    quota_thread.join()

    # while 1:
    #     code = input("请输入需要清算的股票代码(-1所有, q退出)：")
    #     if code == 'q':
    #         break
    #     elif code == '-1':
    #         for c in stocks.all_stocks():
    #             for obj in quotas:
    #                 obj.flush(c)
    #     else:
    #         for obj in quotas:
    #             obj.flush(code)


if __name__ == '__main__':
    main()
